import {
  queryOrderStyleList,
  queryAttachmentTypeList,
  queryOrderQualityList,
  queryAllOrderData,
  queryOrderMakeTeam,
  queryCompanyList,
  queryCustomerInfoList
} from '@/utils/order'

const state = {
  attachmentType: [],             //所有订单附件等级集合
  orderQuality: [],               //所有订单品质类型集合
  orderStyle: [],                 //所有订单风格类型集合
  todayOrderManySum: 0,           //今日订单总数
  todayOrderMoneySum: 0.0,        //今日订单总金额
  monthOrderMoneySum: 0.0,        //本月订单总金额
  totalOrderMoneySum: 0.0,        //总订单金额
  orderTotalCount: 0,             //订单总数
  placeOrderStatusCount: 0,       //下单状态订单数
  successOrderStatusCount: 0,     //完成状态订单数
  failOrderStatusCount: 0,        //死单状态订单数
  companyInformation: [],         //公司集合
  orderMakeTeam: [],              //订单制作小组
  orderCustomer: [],              //客户信息集合
};

const mutations = {
  SET_ATTACHMENT_TYPE: (state, attachmentType) => {
    state.attachmentType = attachmentType;
  },
  SET_ORDER_QUALITY: (state, orderQuality) => {
    state.orderQuality = orderQuality;
  },
  SET_ORDER_STYLE: (state, orderStyle) => {
    state.orderStyle = orderStyle;
  },
  SET_TODAY_ORDER_MANY_SUM: (state, todayOrderManySum) => {
    state.todayOrderManySum = todayOrderManySum;
  },
  SET_TODAY_ORDER_MONEY_SUM: (state, todayOrderMoneySum) => {
    state.todayOrderMoneySum = todayOrderMoneySum;
  },
  SET_MONTH_ORDER_MONEY_SUM: (state, monthOrderMoneySum) => {
    state.monthOrderMoneySum = monthOrderMoneySum;
  },
  SET_TOTAL_ORDER_MONEY_SUM: (state, totalOrderMoneySum) => {
    state.totalOrderMoneySum = totalOrderMoneySum;
  },
  SET_ORDER_TOTAL_COUNT: (state, orderTotalCount) => {
    state.orderTotalCount = orderTotalCount;
  },
  SET_PLACE_ORDER_STATUS_COUNT: (state, placeOrderStatusCount) => {
    state.placeOrderStatusCount = placeOrderStatusCount;
  },
  SET_SUCCESS_ORDER_STATUS_COUNT: (state, successOrderStatusCount) => {
    state.successOrderStatusCount = successOrderStatusCount;
  },
  SET_FAIL_ORDER_STATUS_COUNT: (state, failOrderStatusCount) => {
    state.failOrderStatusCount = failOrderStatusCount;
  },
  SET_COMPANY_INFORMATION: (state, companyInformation) => {
    state.companyInformation = companyInformation;
  },
  SET_ORDER_MAKE_TEAM: (state, orderMakeTeam) => {
    state.orderMakeTeam = orderMakeTeam
  },
  SET_ORDER_CUSTOMER: (state, orderCustomer) => {
    state.orderCustomer = orderCustomer
  },
};

const getters = {};

const actions = {
  queryOrderStyle({commit}) {
    return new Promise((resolve, reject) => {
      queryOrderStyleList().then(data => {
        commit('SET_ORDER_STYLE', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  queryOrderAttachmentType({commit}) {
    return new Promise((resolve, reject) => {
      queryAttachmentTypeList().then(data => {
        commit('SET_ATTACHMENT_TYPE', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  queryOrderQuality({commit}) {
    return new Promise((resolve, reject) => {
      queryOrderQualityList().then(data => {
        commit('SET_ORDER_QUALITY', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  queryCompanyInfo({commit}) {
    return new Promise((resolve, reject) => {
      queryCompanyList().then(data => {
        commit('SET_COMPANY_INFORMATION', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  queryOrderDataObj({commit}) {
    return new Promise((resolve, reject) => {
      queryAllOrderData().then(data => {
        commit('SET_TODAY_ORDER_MANY_SUM', data.todayOrderManySum)
        commit('SET_TODAY_ORDER_MONEY_SUM', data.todayOrderMoneySum)
        commit('SET_MONTH_ORDER_MONEY_SUM', data.monthOrderMoneySum)
        commit('SET_TOTAL_ORDER_MONEY_SUM', data.totalOrderMoneySum)
        commit('SET_ORDER_TOTAL_COUNT', data.orderTotalCount)
        commit('SET_PLACE_ORDER_STATUS_COUNT', data.placeOrderStatusCount)
        commit('SET_SUCCESS_ORDER_STATUS_COUNT', data.successOrderStatusCount)
        commit('SET_FAIL_ORDER_STATUS_COUNT', data.failOrderStatusCount)

        //储存到sessionStorage中 防止刷新页面数据消失
        sessionStorage.setItem("SET_ORDER_TOTAL_COUNT", data.orderTotalCount)
        sessionStorage.setItem('SET_PLACE_ORDER_STATUS_COUNT', data.placeOrderStatusCount)
        sessionStorage.setItem('SET_SUCCESS_ORDER_STATUS_COUNT', data.successOrderStatusCount)
        sessionStorage.setItem('SET_FAIL_ORDER_STATUS_COUNT', data.failOrderStatusCount)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  queryOrderTeam({commit}) {
    return new Promise((resolve, reject) => {
      queryOrderMakeTeam().then(data => {
        commit('SET_ORDER_MAKE_TEAM', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  queryCustomerInformation({commit}) {
    return new Promise((resolve, reject) => {
      queryCustomerInfoList().then(data => {
        commit('SET_ORDER_CUSTOMER', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
};


export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
}
